package com.yumei.trader.gym.net

import android.util.Log
import com.yumei.trader.gym.BuildConfig
import com.yumei.trader.gym.`object`.BaseResponse
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.GET
import retrofit2.http.Header
import java.util.concurrent.TimeUnit


class NetWork {
    companion object {
        private val interceptor = Interceptor { chain ->
            val request = chain.request()
            val requestBuilder = request.newBuilder()
            val url = request.url()
            val builder = url.newBuilder()
            requestBuilder.url(builder.build())
                    .addHeader("terry","tradercode")//appcode
                    .addHeader("multiple", BuildConfig.VERSION_CODE.toString())//versioncode
                    .method(request.method(), request.body())
            chain.proceed(requestBuilder.build())
        }
        private val logInterceptor = HttpLoggingInterceptor(HttpLogger())
        private val client: OkHttpClient.Builder = OkHttpClient.Builder()
                .addInterceptor(interceptor)
                .connectTimeout(30, TimeUnit.SECONDS)
                .addNetworkInterceptor(logInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY))
                .readTimeout(30, TimeUnit.SECONDS)
                .retryOnConnectionFailure(false)


        private var retrofit: Retrofit = Retrofit.Builder()
                .baseUrl(BuildConfig.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(client.build())
                .build()

        var service: RequestService = retrofit.create(RequestService::class.java)

    }

    class HttpLogger : HttpLoggingInterceptor.Logger {
        override fun log(message: String) {
            Log.d("HttpLogInfo", message)
        }
    }

    interface RequestService {
        @GET("trade-domains/ypmq/xmymo")
        fun getGuide(@Header("inlet") androidId: String,@Header("introduction") afId: String?): Call<BaseResponse<String>>
    }
}